<?php
namespace BP\En;
require_once("java/util/ArrayList.php");
require_once("java/util/List.php");
require_once("BP/DA/DataType.php");
class EntitiesTree extends Entities {
	protected static $serialVersionUID;	// long
	public $appendMenus;	// StringBuilder
	public $appendMenuSb;	// StringBuilder
	private function __init() { // default class members
		$this->appendMenus = NULL;
		$this->appendMenuSb = NULL;
	}
	public static function __staticinit() { // static class members
		self::$serialVersionUID = 1;
	}
	public static function convertEntityTree ($obj) // [Object obj]
	{
		return $obj;
	}
	public function ToJavaListEnTree () 
	{
		return $this;
	}
	public function ToJsonOfTree ($rootNo) // [String rootNo]
	{
		if (($DataType->IsNullOrEmpty($rootNo) ==  TRUE ))
		{
			$rootNo = "0";
		}
		$this->appendMenus = new StringBuilder();
		$this->appendMenuSb = new StringBuilder();
		$root = $this->GetEntityByKey(EntityTreeAttr::$ParentNo, $rootNo);
		if (($root == NULL))
			return (("err@没有找到rootNo=" . $rootNo) . "的entity.");
		$this->appendMenus->append("[{");
		$this->appendMenus->append((("'id':'" . $root->getNo()) . "',"));
		$this->appendMenus->append((("'pid':'" . $root->getParentNo()) . "',"));
		$this->appendMenus->append((("'text':'" . $root->getName()) . "'"));
		$this->appendMenus->append(",'children':");
		$this->AddChildren($root, $this);
		$this->appendMenus->append($this->appendMenuSb);
		$this->appendMenus->append("}]");
		return $this->ReplaceIllgalChart($this->appendMenus);
	}
	public function AddChildren ($parentEn, $ens) // [EntityTree parentEn, EntitiesTree ens]
	{
		$this->appendMenus->append($this->appendMenuSb);
		$this->appendMenuSb->delete(0, $this->appendMenuSb->length());
		$this->appendMenuSb->append("[");
		foreach ($ens->ToJavaListEnTree() as $item) 		{
			if (!$item->getParentNo()->equals($parentEn->getNo()))
				continue;
			$this->appendMenuSb->append((((((("{'id':'" . $item->getNo()) . "','pid':'") . $item->getParentNo()) . "','text':'") . $item->getName()) . "'"));
			$treeNode = $item;
			$this->appendMenuSb->append(",'children':");
			$this->AddChildren($item, $ens);
			$this->appendMenuSb->append("},");
		}
		if (($this->appendMenuSb->length() > 1))
			$this->appendMenuSb = $this->appendMenuSb->deleteCharAt(($this->appendMenuSb->length() - 1));
		$this->appendMenuSb->append("]");
		$this->appendMenus->append($this->appendMenuSb);
		$this->appendMenuSb->delete(0, $this->appendMenuSb->length());
	}
	public function ReplaceIllgalChart ($s) // [String s]
	{
		$sb = new StringBuilder();
		for ($i = 0,$j = $s->length(); ($i < $j); ++$i) 
		{
// 			$c = $s->charAt($i);
			$c = substr($s, $i, 1);
			
			switch ($c) {
				case '\"':
					$sb->append("\\\"");
					break;
				case '\\':
					$sb->append("\\\\");
					break;
				case '/':
					$sb->append("\\/");
					break;
				case '\b':
					$sb->append("\\b");
					break;
				case '\f':
					$sb->append("\\f");
					break;
				case '\n':
					$sb->append("\\n");
					break;
				case '\r':
					$sb->append("\\r");
					break;
				case '\t':
					$sb->append("\\t");
					break;
				default:
					$sb->append($c);
					break;
			}
		}
		return $sb;
	}
	public function RetrieveHisChinren ($en) // [EntityTree en]
	{
		$i = $this->Retrieve(EntityTreeAttr::$ParentNo, $en->getNo());
		$this->AddEntity($en);
		return ($i + 1);
	}
	public function GenerHisChinren ($en) // [EntityTree en]
	{
		$tempVar = $this->CreateInstance();
		$ens = (( (($tempVar instanceof EntitiesTree)) ? $tempVar : NULL ));
		foreach (EntitiesTree::convertEntityTree($ens) as $item) 		{
			if ($en->getNo()->equals($en->getParentNo()))
			{
				$ens->AddEntity($item);
			}
		}
		return $ens;
	}
	public function getItem ($index) // [int index]
	{
		return $this->get($index);
	}
	public static function constructor__ () 
	{
		$me = new self();
		$me->__init();
		return $me;
	}
}
EntitiesTree::__staticinit(); // initialize static vars for this class on load
?>
